package basic01.arrys;

import java.util.Arrays;

/**
 * @author Mark
 * @version 1.0
 * @date 2020/5/16 18:00
 * @description 数组
 */
public class ArrayTest {
    public static void main(String[] args) {
        int[] ints = {1, 2, 3, 4, 5, 6, 2, 34, 345, 5345};
/*        System.out.println(Arrays.toString(ints));
        testArray();
        array();
        findMax();
        printArray(ints);*/
        System.out.println(Arrays.toString(sort(ints)));
    }

    /**
     * 测试数组数据 增强for循环
     */
    private static void testArray() {
        int sum = 0;
        int[] ints = {1, 2, 3, 4, 5, 6};
        for (int anInt : ints) {
            sum = sum + anInt;
        }
        System.out.println("sum = " + sum);
    }

    /**
     * 测试数组数据 for循环
     */
    private static void array() {
        int[] ints = {1, 2, 3, 4, 5, 6};
        int sum = 0;
        for (int i = 0; i < ints.length; i++) {
            sum = sum + ints[i];
        }
        System.out.println("sum = " + sum);
    }

    /**
     * 查找最大值
     */
    private static void findMax() {
        int[] ints = {1, 2, 3, 4, 5, 6};
        int max = ints[0];
        for (int anInt : ints) {
            if (anInt > max) {
                max = anInt;
            }
        }
        System.out.println("max = " + max);
    }

    /**
     * 打印数组
     *
     * @param arrays 数组
     */
    private static void printArray(int[] arrays) {
        for (int array : arrays) {
            System.out.print(array + " ");
        }
    }

    /**
     * 冒泡排序
     *
     * @param arrays
     * @return 数组
     */
    private static int[] sort(int[] arrays) {
        int temp;
        for (int i = 0; i < arrays.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < arrays.length - 1 - i; j++) {
                if (arrays[j + 1] < arrays[j]) {
                    temp = arrays[j];
                    arrays[j] = arrays[j + 1];
                    arrays[j + 1] = temp;
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
        return arrays;
    }
}
